package cn.com.kh;

import cn.com.elite.Util;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class LZT_Test {
    private static final Log log = LogFactory.getLog(LZT_Test.class);

    public static void main(String[] args) throws Exception {

        String url = "http://sms3.mobset.com";//请求http的路径如：http://sms3.mobset.com
        String CorpID = "301195";//企业ID3*****3
        String LoginName = "400";//用户名wu**
        String SecretKey = "734095";//密码W*****5

        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("MMddHHmmss");//日期格式化
        String timeStamp = dateFormat.format(now);
        String strPwsd = MD5.md5(CorpID + SecretKey + timeStamp);//MD5加密方式
        //Methods = SmsSend为发送短信，SmsGetRecv取回复短信，SmsGetReport取短信状态，SmsGetBalance取账号剩余短信量,SmsGetSign取短信签名。
        String ip = url + ":8095/SDK3/Sms?Methods=SmsGetRecv&CorpID=" + CorpID + "&LoginName=" + LoginName + "&TimeStamp=" + timeStamp + "&SecretKey=" + strPwsd;
        System.out.println(ip.toString());
        //String sr = sendPost(ip);

        //System.out.println(sr.toString());

        url = "http://sms3.mobset.com:8095/SDK3/Sms";

        StringBuffer formdata = new StringBuffer();
        //参数拼接
        formdata.append("Methods=SmsGetRecv");
        formdata.append("&CorpID=" + CorpID);
        formdata.append("&LoginName=" + LoginName);
        formdata.append("&TimeStamp=" + timeStamp);
        formdata.append("&SecretKey=" + strPwsd);
        log.debug("formdata:" + formdata);

        String resJson = Util.doHttpPost(url, formdata.toString(), "UTF-8", 10000, 1);

        log.debug(resJson);
    }

    public static String sendPost(String url) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "utf-8"));
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！" + e);
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }

}
